package net.comm.pic;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;

import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class Zoompic {

	
	// 添加水印,filePath 源图片路径， watermark 水印图片路径 
	public static  boolean  createMark(String filePath,String watermark)   {
		ImageIcon imgIcon = new  ImageIcon(filePath);
		Image theImg  = imgIcon.getImage();
		ImageIcon waterIcon = new  ImageIcon(watermark);
		Image waterImg  = waterIcon.getImage();
		int  width = theImg.getWidth( null );
		int  height =  theImg.getHeight( null );
	
		BufferedImage bimage  =   new  BufferedImage(width,height, BufferedImage.TYPE_INT_RGB); 
	
		Graphics2D g = bimage.createGraphics();
		g.setColor(Color.red);
		g.setBackground(Color.white);
		g.drawImage(theImg,  0 ,  0 ,  null  );
		g.drawImage(waterImg,  100 ,  100 ,  null  );
		g.drawString( " fcwr " , 10 , 10 );  // 添加文字 
		g.dispose();
		try  {
			FileOutputStream out = new  FileOutputStream(filePath);
			JPEGImageEncoder encoder  = JPEGCodec.createJPEGEncoder(out); 
			JPEGEncodeParam param  =  encoder.getDefaultJPEGEncodeParam(bimage); 
			param.setQuality(50f,  true ); 
			encoder.encode(bimage, param); 
			out.close();
		} catch (Exception e)  {  return   false ; } 
			return   true ;	
	} 	
	  /**   
	   *   图片转换功能   
	   *   @param   width   int　　　　　　　　　 目标图片宽度   
	   *   @param   height   int　　　　　　　　　目标图片高度   
	   *   @param   fromDir   String　　　　　　　源文件夹   
	   *   @param   fromFile   String　　　　　　源文件名   
	   *   @param   toDir   String　　　　　　　　目标文件夹   
	   *   @param   toSmallFile   String　　　　　目标文件名   
	   *   @param   chanageFormat   String　　　　JPG     PNG       BMP       GIF   
	   *   @param   ratioType   int　　　　　　　　为0时受比例限制,为1时不受比例限制强行按给定尺寸进行转换   
	   *   @param   isFromFileDel   int　　　　　　0表示不删除原文件,1表示删除原文件;   
	   *   @return   int　　　　　　　　　　　　　1成功0失败   
	   */   
	  public static boolean SmallPic(int width, int height, String fromDir, String fromFile,   
	                                         String toDir, String toSmallFile, String chanageFormat,   
	                                         int ratioType,   int isFromFileDel)  
	  {   
	     try  {   
//	         int pos = fromFile.lastIndexOf('.');
//	         String   Imagetype   =   fromFile.substring(pos+1, fromFile.length()); 
	         String   Imagetype = chanageFormat;
	         File   fF   =   new   File(fromDir,   fromFile);   
	         File   tF   =   new   File(toDir,   toSmallFile);   
//	         System.out.println("ft"+ fromDir+fromFile);
//	         System.out.println("ftt"+ toDir+toSmallFile);
	         
	         
	         BufferedImage   bi   =   ImageIO.read(fF);   
	         
	         int   pwidth   =   (int)   (bi.getWidth());   
	         int   pheight   =   (int)   (bi.getHeight());   
	         double   ratio   =   1.0000;   
	         double   ratioX   =   1.0000;   
	         double   ratioY   =   1.0000;   
	         Image   Itemp   =   bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);   

	         if   (ratioType   !=   1)   {   
	             if   (   (pwidth   >   width)   ||   (pheight   >   height))   {   
	                 double   ratioO   =   1.0000;   
	                 double   ratioN   =   1.0000;   
	                 ratioO   =   (double)   height   /   width;   
	                 ratioN   =   (double)   pheight   /   pwidth;   
	                 double   Nheight   =   0;   
	                 double   Nwidth   =   0;   
	                 if   (ratioN   >   ratioO)   {   
	                     //超高的   
	                     Nheight   =   height;   
	                     Nwidth   =   height   /   ratioN;   
	                 }   
	                 else   {   
	                     //超宽   
	                     Nwidth   =   width;   
	                     Nheight   =   width   *   ratioN;   
	                 }   
	                 ratioX   =   Nwidth   /   pwidth;   
	                 ratioY   =   Nheight   /   pheight;   

	             }   
	         }   
	         else   {   
	             ratioX   =   (double)   width   /   pwidth;   
	             ratioY   =   (double)   height   /   pheight;   
//	                   ratioX=0.55;   
//	                   ratioY=0.43;   

	         }   
	         AffineTransformOp op = new AffineTransformOp(AffineTransform.   
	                 getScaleInstance(ratioX,   ratioY),   null);   
	         Itemp = op.filter(bi,   null);   
	         ImageIO.write((BufferedImage)Itemp, Imagetype, tF);   
	         int   fFlen = (int)fF.length();   
	         if(isFromFileDel == 1)   {   
	             fF.delete();   
	         }   
	         return true;   
	     }catch (Exception e)   {   

	         /*   
	                       Log   log   =   Log.getInstance();   
	                       log.Addlog("Mobile转换图片出错(mobileImage.chanageMobPic.SmallPic)："   +   
	                               e.getMessage());   
	           */   

	         e.printStackTrace();   
	         return  false;   
	     }   
	  } 	

	
	  /** 
	  * 图片切割 
	  * @param srcImageFile 图片地址 
	  * @param responseOutputStream servlet输出流 
	  * @param w 切割宽度 
	  * @param h 切割高度 
	  * @param x1 开始x结点（left） 
	  * @param y1 开始y结点（top） 
	  * @param sw 图片宽度 
	  * @param sh 图片高度 
	  */ 
	  public static boolean cut(String fromDir, String srcImageFile, String toDir, 
			  String toSmallFile, int w, int h, int x1, int y1, int sw, int sh) { 
	  try { 
//		  System.out.println("f=" + fromDir + srcImageFile);
//		  System.out.println("t=" + toDir + toSmallFile);
		File fF =   new   File(fromDir,   srcImageFile);   
		File tF =   new   File(toDir,   toSmallFile);   

		Image img; 
	
		ImageFilter cropFilter; 
		// 读取源图像 
		BufferedImage bi = ImageIO.read(fF); 
	
		if (sw >= w && sh >= h) { 
	
		  Image image = bi.getScaledInstance(sw, sh, Image.SCALE_DEFAULT); 
	
		  // 剪切起始坐标点 
		  int x = x1; 
		  int y = y1; 
	
		  int destWidth = w; // 切片宽度 
		  int destHeight = h; // 切片高度 
	
		  // 图片比例 
		  double pw = sw; 
		  double ph = sh; 
	
		  double m = (double) sw / pw; 
		  double n = (double) sh / ph; 
	
		  int wth = (int) (destWidth * m); 
		  int hth = (int) (destHeight * n); 
		  int xx = (int) (x * m); 
		  int yy = (int) (y * n); 
	
		  // 四个参数分别为图像起点坐标和宽高 
		  // 即: CropImageFilter(int x,int y,int width,int height) 
	
		  cropFilter = new CropImageFilter(xx, yy, wth, hth); 
		  img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter)); 
	
		  
		  BufferedImage tag = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 
		  Graphics g = tag.getGraphics(); 
		  g.drawImage(img, 0, 0, null); // 绘制缩小后的图 
		  g.dispose(); 
	
		  // 输出为文件 
		  ImageIO.write((BufferedImage)tag, "JPG", tF); 
		}else{
			System.out.println("裁剪宽度"+w+"大于照片宽度"+sw);
		}
	  } catch (Exception e) { 
		  e.printStackTrace(); 
	  } 
	  return true;
	} 
	
}
